package com.example.esredisrabbitmqdemo.message;

import com.example.esredisrabbitmqdemo.dao.UserMapper;
import com.example.esredisrabbitmqdemo.pojo.User;
import com.example.esredisrabbitmqdemo.service.UserService;
import com.example.esredisrabbitmqdemo.service.UserServiceForElasticsearch;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Component;

/**
 * @BelongsProject: es-redis-rabbitmq-demo
 * @BelongsPackage: com.example.esredisrabbitmqdemo.message
 * @Author: 江岸
 * @CreateTime: 2020-07-04 15:38
 * @Description:消息消费者
 */
@Component
public class MessageListener {
    @Autowired
    UserService userService;
    @Autowired
    UserServiceForElasticsearch userServiceES;


    @RabbitListener(queuesToDeclare = @Queue("userId"))//接受消息。去数据库查到数据放到ES中
    public void listen(String msg) throws InterruptedException {
        System.out.println("接收到消息：" + msg);
        int id = Integer.parseInt(msg);
        //先删除ES里面的
        User user = new User();
        user.setId(id);
        userServiceES.delete(user);
        //再去查询新的
        user = userService.queryById(id);
        userServiceES.save(user);
    }

    //接受消息。删除ES中该id的对象
    @RabbitListener(queuesToDeclare = @Queue("userIdDel"))
    public void listen2(String msg) throws InterruptedException {
        System.out.println("接收到消息删除：" + msg);
        int id = Integer.parseInt(msg);
        User user = new User();
        user.setId(id);
        userServiceES.delete(user);
    }
}
